// pages/MyFollow/MyFollow.js
let app = getApp()
let db = wx.cloud.database()
let beginTime = new Date()
let endTime = new Date()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        currentPage: 0,
    },
    getBoardGame() {
        db.collection('bgDB').get({
            success: (res) => {
                let bgArr = res.data
                bgArr.forEach((item) => {
                    item['selected'] = false
                })
                this.setData({bgArr})
            }
        })
    },
    
    selectBoardGame(e) {
        let index = e.currentTarget.dataset.index
        this.setData({[`bgArr[${index}].selected`]: !this.data.bgArr[index].selected})
    },


    pageChange(e) {
        if (e.detail.source == "touch")
            this.setData({currentPage: e.detail.current})
    },
    previousPage(e) {
        let currentPage = this.data.currentPage - 1
        this.setData({currentPage}) 
    },
    nextPage(e) {
        let currentPage = this.data.currentPage + 1
        this.setData({currentPage}) 
    },
    showTimeActionSheet(e) {
        let title = e.currentTarget.dataset.title
        if (this.data.showTimeActionSheet != true) {
            this.setData({showTimeActionSheet: true,title})
        }
    },
    closeTimeActionSheet() {
        this.setData({showTimeActionSheet: false})
    },
    getActivityDay() {
        let date = new Date(beginTime)
        let activityDay = [
            {'name': '今天', 'string': '', 'month': '' ,'day': '', 'selected': true},
            {'name': '明天', 'string': '', 'month': '' ,'day': '', 'selected': false},
            {'name': '后天', 'string': '', 'month': '' ,'day': '', 'selected': false},
        ]
        activityDay.forEach((item) => {
            let month = (date.getMonth() + 1) < 10 ?  '0' + (date.getMonth() + 1) : (date.getMonth() + 1)
            let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
            item.string = month + '-' + day
            item.month = date.getMonth() + 1
            item.day = date.getDate()
            date.setDate(date.getDate() + 1)
        })
        this.setData({activityDay})
    },
    getActivityTime() {
        let activityTime = []
        for (let i = 0; i < 24; i++) {
            activityTime.push({'time': (i < 10 ? '0' + i : i) + ':00', 'hour': i, 'minute': 0, 'selected': false})
            activityTime.push({'time': (i < 10 ? '0' + i : i) + ':30', 'hour': i, 'minute': 30, 'selected': false})
        }
        // 默认时间
        let hour = beginTime.getHours()
        activityTime.forEach((item) => {
            if (hour >= 30) {
                if (item.time.substr(0,2) == hour + 1 && item.time.substr(3,5) == '00') {
                    item.selected = true
                    beginTime.setHours(hour.getHours() + 1)
                    beginTime.setMinutes(0)
                    endTime.setHours(hour.getHours() + 1)
                    endTime.setMinutes(0)
                }
            } else {
                if (item.time.substr(0,2) == hour && item.time.substr(3,5) == '30') {
                    item.selected = true
                    beginTime.setMinutes(30)
                    endTime.setMinutes(30)
                }
            }
        })
        this.setData({activityTime})
    },
    selectDay(e) {
        let index = e.currentTarget.dataset.index
        let activityDay = this.data.activityDay
        activityDay.forEach(item => item.selected = false)
        activityDay[index].selected = true

        let title = this.data.title
        if (title == "begin") {
            beginTime.setMonth(activityDay[index].month - 1)
            beginTime.setDate(activityDay[index].day)
            this.setData({
                beginTime: beginTime.getFullYear() + '-' + (beginTime.getMonth() + 1) + '-' + beginTime.getDate() + '  ' + beginTime.getHours() + ':' + beginTime.getMinutes()
            })
        } else {
            endTime.setMonth(activityDay[index].month - 1)
            endTime.setDate(activityDay[index].day)
            this.setData({
                endTime: endTime.getFullYear() + '-' + (endTime.getMonth() + 1) + '-' + endTime.getDate() + '  ' + endTime.getHours() + ':' + endTime.getMinutes()
            })
        }
        this.setData({activityDay})
    },
    selectTime(e) {
        let index = e.currentTarget.dataset.index
        let activityTime = this.data.activityTime
        activityTime.forEach(item => item.selected = false)
        activityTime[index].selected = true

        let title = this.data.title
        if (title == "begin") {
            beginTime.setHours(activityTime[index].hour)
            beginTime.setMinutes(activityTime[index].minute)
            this.setData({
                beginTime: beginTime.getFullYear() + '-' + (beginTime.getMonth() + 1) + '-' + beginTime.getDate() + '  ' + beginTime.getHours() + ':' + beginTime.getMinutes()
            })
        } else {
            endTime.setHours(activityTime[index].hour)
            endTime.setMinutes(activityTime[index].minute)
            this.setData({
                endTime: endTime.getFullYear() + '-' + (endTime.getMonth() + 1) + '-' + endTime.getDate() + '  ' + endTime.getHours() + ':' + endTime.getMinutes()
            })
        }
        this.setData({activityTime})
    },
    changeLocation(e) {
        wx.chooseLocation({
            success: (res) => {
                let markers = [{
                    latitude: res.latitude,
                    longitude: res.longitude,
                    title: res.name
                }]
                this.setData({
                    location: res.name,
                    address: res.address,
                    latitude: res.latitude,
                    longitude: res.longitude,
                    markers: markers
                })
            }
          })
    },
    changeName(e){
        let arr = ['一起来玩','一局','我爱','有没有人来']
        let nameNumber = Math.round(Math.random() * 4)
        if (nameNumber >= arr.length)
            nameNumber = 0
        let bgArr = []
        this.data.bgArr.forEach(item => item.selected ? bgArr.push(item) : '')
        let bgNumber = Math.round(Math.random() * bgArr.length)
        if (bgNumber >= bgArr.length)
            bgNumber = 0
        this.setData({
            activity:arr[nameNumber] + bgArr[bgNumber].name
        })
    },
    uploadImage() {
        let that = this
        wx.chooseMedia({
            count: 1,
            mediaType: ['image'],
            sourceType: ['album', 'camera'],
            maxDuration: 30,
            success: (res) => {
                let image = res.tempFiles[0].tempFilePath
                this.setData({image})
                wx.cloud.uploadFile({
                    cloudPath: 'activity/' + (new Date()).valueOf() + ".jpg",
                    filePath: this.data.image, // 文件路径
                }).then(res => {
                    let myImage = res.fileID
                    console.log(myImage);
                    that.setData({myImage})
                })
            }
        })
    },
    publishActivity(e) {
        let that = this
        let bgArr = []
        this.data.bgArr.forEach(item => {
            if (item.selected == true)
                bgArr.push(item)
        })
        
        let data = {
            organizer: app.globalData.openid,
            status: "ready",
            activity: this.data.activity,
            beginTime: beginTime,
            endTime: endTime,
            boardgame: bgArr,
            location: this.data.location,
            address: this.data.address,
            longitude: this.data.longitude,
            latitude: this.data.latitude,
            people: {},
            image: this.data.myImage,
            createTime: db.serverDate()
        }

        db.collection("activityDB").add({
            data: data,
            success: (res) => {
                wx.navigateBack({
                  delta: 0,
                })
            },
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getBoardGame()
        this.getActivityDay()
        this.getActivityTime()
        wx.getFuzzyLocation({
            success: (res) => {
                let markers = {
                    id: 233,
                    latitude: res.latitude,
                    longitude: res.longitude,
                    iconPath: "../../images/地图定位1.png",
                    width:  30,
                    height: 30
                }
                this.setData({
                    longitude: res.longitude,
                    latitude: res.latitude,
                    markers: markers
                })
            }
            
        })
    },
    
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})